package com.spa.interfaces.massagist;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.spa.application.command.massagist.MassagistMyEditCommand;
import com.spa.application.dto.massagist.MassagistFinanceDTO;
import com.spa.application.dto.massagist.MassagistFinanceDetailBaseDTO;
import com.spa.application.dto.massagist.MassagistFinanceDetailDTO;
import com.spa.application.query.massagist.MassagistFinanceQuery;
import com.spa.application.query.massagist.MassagistQuery;
import com.spa.application.service.massagist.MassagistHomeApplicationService;
import com.spa.infrastructure.common.ResponseMessage;
import com.spa.infrastructure.exception.SpaException;
import com.spa.interfaces.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("massagist/my")
public class MassagistInfoController extends BaseController {

    @Autowired
    private MassagistHomeApplicationService messagistApplicationService;

    @PostMapping("/query/info/finance")
    public ResponseMessage<MassagistFinanceDTO> queryMassagistFinance(@RequestBody MassagistQuery query) {
        MassagistFinanceDTO myFinance = messagistApplicationService.getMyFinance(query);
        return responseHelper.success(myFinance);
    }

    @PostMapping("/submit/info")
    public ResponseMessage submitMassagistInfo(@RequestBody MassagistMyEditCommand command) {
        messagistApplicationService.myInfoChange(command);
        return responseHelper.success();
    }

    @PostMapping("/query/finance")
    public ResponseMessage<MassagistFinanceDetailDTO> queryMassagistFinance(@RequestBody MassagistFinanceQuery query) {
        MassagistFinanceDetailDTO financeDetailDTO =  messagistApplicationService.getMyFinanceDetails();
        return responseHelper.success(financeDetailDTO);
    }

    @PostMapping("/query/finance/daily")
    public ResponseMessage<MassagistFinanceDetailBaseDTO> queryMassagistDailyFinance(@RequestBody MassagistFinanceQuery query) {
        if (DateUtil.between(query.getStartTime(), query.getEndTime(), DateUnit.DAY) > 30){
            throw new SpaException("查询日期最大为30天");
        }
        List<MassagistFinanceDetailBaseDTO> financeDetailDTO =  messagistApplicationService.getMyFinanceDailyDetails(query);
        return responseHelper.success(financeDetailDTO);
    }

}
